package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;

	@PostMapping("saveAccount/{userId}")
	public Result saveAccount(@PathVariable Long userId){
		this.userAccountService.saveAccount(userId);
		return Result.ok();
	}
	// 查询用户余额
	// http://127.0.0.1:8500/api/account/userAccount/getAvailableAmount
	@GuiguLogin
	@Operation(summary = "查询用户余额")
	@GetMapping("getAvailableAmount")
	public Result getAvailableAmount(){
		BigDecimal availableAmount = userAccountService.getAvailableAmount(AuthContextHolder.getUserId());
		return Result.ok(availableAmount);
	}

	// 远程调用 检查还账户余额是否充足并且扣除余额
	// api/account/userAccount/checkAndDeductAmount
	@Operation(summary = "扣除余额")
	@PostMapping("checkAndDeductAmount")
	public Result<Boolean> checkAndDeductAmount(@RequestBody AccountLockVo accountLockVo){
		Boolean flag = userAccountService.checkAndDeductAmount(accountLockVo);
		return Result.ok(flag);
	}
}

